package com.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.inter.newsInter;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * Servlet implementation class deleteNews
 */
@WebServlet("/deleteNews")
public class deleteNews extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteNews() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
/*	弃用服务编码
 * 		//确认接收编码
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//建立数据库会话链接
		SqlSession deleteSession=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession();
		//建立接口映射
		newsInter mapper=deleteSession.getMapper(newsInter.class);
		//执行删除
		int deleteId=mapper.deleteNews(Integer.parseInt(request.getParameter("id")));
		deleteSession.commit();
		response.sendRedirect("selectNew");*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//确认接收编码
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获取sqlSession
		SqlSession sqlSession = SqlSessionFactoryUtil.openSqlSession();
		//建立接口映射
		newsInter mapper=sqlSession.getMapper(newsInter.class);
		//执行删除
		int deleteId=mapper.deleteNews(Integer.parseInt(request.getParameter("id")));
		sqlSession.commit();
		sqlSession.close();
		//建立输出流
		PrintWriter out=response.getWriter();
		if(deleteId!=0){
			out.write("删除成功");
		}else{
			out.write("删除失败");
		}
	}

}
